//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.vere.orm.db;

import com.vere.orm.db.pool.ConnectionPool;
import com.vere.orm.model.ClassItem;
import com.vere.orm.model.Property;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Configuration {
    private static ConcurrentHashMap<String, String> beanMap = new ConcurrentHashMap();
    private static ConcurrentHashMap<String, String> dataSourceMap = new ConcurrentHashMap();
    private static ConcurrentHashMap<String, String> constantsMap = new ConcurrentHashMap();
    private static ConcurrentHashMap<String, ClassItem> mappingMap = new ConcurrentHashMap();
    private static ConnectionPool dataSource = null;
    private static String classAbsolutePath = "";
    private static String configFileName = "VereORM.xml";
    private static String connenctionPoolClass = "";

    static {
        Object obj = new Object();
        if (classAbsolutePath == null || "".equals(classAbsolutePath)) {
            URL url = obj.getClass().getResource("/");
            if (url == null) {
                url = Thread.currentThread().getContextClassLoader().getResource("");
            }

            classAbsolutePath = url.getPath();
            loadResources();
        }

    }

    public Configuration() {
    }

    private void init() {
    }

    public static ConnectionPool getDataSource() {
        if (dataSource == null) {
            Class clazz = null;

            try {
                clazz = Class.forName(getConnenctionPoolClass());
                dataSource = (ConnectionPool)clazz.newInstance();
            } catch (ClassNotFoundException var2) {
                var2.printStackTrace();
            } catch (InstantiationException var3) {
                var3.printStackTrace();
            } catch (IllegalAccessException var4) {
                var4.printStackTrace();
            }
        }

        return dataSource;
    }

    public static Object getBean(String beanName) {
        String clazzName = (String)beanMap.get(beanName);
        Class clazz = null;
        Object obj = null;

        try {
            clazz = Class.forName(clazzName);
            obj = clazz.newInstance();
        } catch (ClassNotFoundException var5) {
            var5.printStackTrace();
        } catch (InstantiationException var6) {
            var6.printStackTrace();
        } catch (IllegalAccessException var7) {
            var7.printStackTrace();
        }

        return obj;
    }

    public static String getDataSourcePropery(String name) {
        String value = (String)dataSourceMap.get(name);
        return value;
    }

    public static void loadResources() {
        String currentClassPath = getClassAbsolutePath();
        if (currentClassPath != null && !"".equals(currentClassPath)) {
            FileInputStream is = null;

            try {
                SAXReader reader = new SAXReader();
                String filePath = getClassAbsolutePath() + configFileName;
                is = new FileInputStream(filePath);
                if (is == null) {
                    return;
                }

                Document doc = reader.read(is);
                List<Element> elementList = doc.selectNodes("/VereORM/bean");
                Iterator var7 = elementList.iterator();

                Element element;
                String className;
                String table;
                while(var7.hasNext()) {
                    element = (Element)var7.next();
                    className = element.attributeValue("id");
                    table = element.attributeValue("class");
                    beanMap.put(className, table);
                }

                elementList = doc.selectNodes("/VereORM/constant");
                var7 = elementList.iterator();

                while(var7.hasNext()) {
                    element = (Element)var7.next();
                    className = element.attributeValue("name");
                    table = element.attributeValue("value");
                    constantsMap.put(className, table);
                }

                elementList = doc.selectNodes("/VereORM/dataSource");

                for(var7 = elementList.iterator(); var7.hasNext(); connenctionPoolClass = element.attributeValue("class")) {
                    element = (Element)var7.next();
                }

                elementList = doc.selectNodes("/VereORM/dataSource/property");
                var7 = elementList.iterator();

                while(var7.hasNext()) {
                    element = (Element)var7.next();
                    className = element.attributeValue("name");
                    table = element.attributeValue("value");
                    dataSourceMap.put(className, table);
                }

                elementList = doc.selectNodes("/VereORM/mapping/class");
                var7 = elementList.iterator();

                while(var7.hasNext()) {
                    element = (Element)var7.next();
                    className = element.attributeValue("name");
                    table = element.attributeValue("table");
                    ClassItem classItem = (ClassItem)mappingMap.get(className);
                    if (classItem == null) {
                        classItem = new ClassItem();
                    }

                    classItem.setName(className);
                    classItem.setTable(table);
                    ConcurrentHashMap<String, Property> propertyMap = classItem.getPropertyMap();
                    if (propertyMap == null) {
                        propertyMap = new ConcurrentHashMap();
                    }

                    List<Element> elementChildList = element.elements();

                    Property property;
                    String name;
                    for(Iterator var14 = elementChildList.iterator(); var14.hasNext(); propertyMap.put(name, property)) {
                        Element elementChild = (Element)var14.next();
                        property = new Property();
                        String propertyName = elementChild.getName();
                        name = elementChild.attributeValue("name");
                        String column = elementChild.attributeValue("column");
                        String generator = elementChild.attributeValue("generator");
                        String type = element.attributeValue("type");
                        String length = element.attributeValue("length");
                        if ("id".equals(propertyName)) {
                            property.setGenerator(generator);
                            property.setKey(true);
                            classItem.setKeyName(name);
                            classItem.setGenerator(generator);
                        }

                        property.setName(name);
                        property.setColumn(column);
                        property.setType(type);
                        if (length == null && "".equals(length)) {
                            property.setLength(Long.parseLong(length));
                        }
                    }

                    classItem.setPropertyMap(propertyMap);
                    mappingMap.put(className, classItem);
                }
            } catch (Exception var31) {
                var31.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException var30) {
                    var30.printStackTrace();
                }

            }

        }
    }

    public static String getKeyName(String className) {
        if (className != null && !"".equals(className)) {
            ClassItem classItem = (ClassItem)mappingMap.get(className);
            return classItem == null ? null : classItem.getKeyName();
        } else {
            return null;
        }
    }

    public static String getGenerator(String className) {
        if (className != null && !"".equals(className)) {
            ClassItem classItem = (ClassItem)mappingMap.get(className);
            return classItem == null ? null : classItem.getGenerator();
        } else {
            return null;
        }
    }

    public static ClassItem getClassItem(String className) {
        if (className != null && !"".equals(className)) {
            ClassItem classItem = (ClassItem)mappingMap.get(className);
            return classItem;
        } else {
            return null;
        }
    }

    public static String getClassAbsolutePath() {
        return classAbsolutePath;
    }

    public static void setClassAbsolutePath(String classAbsolutePath) {
        Configuration.classAbsolutePath = classAbsolutePath;
    }

    public static boolean isDevelopMode() {
        String developMode = (String)constantsMap.get("developMode");
        return "true".equals(developMode);
    }

    public static String getConnenctionPoolClass() {
        if (connenctionPoolClass == null || "".equals(connenctionPoolClass)) {
            connenctionPoolClass = "com.vere.orm.db.pool.NoPoolConnection";
        }

        return connenctionPoolClass;
    }

    public static void setConnenctionPoolClass(String connenctionPoolClass) {
        Configuration.connenctionPoolClass = connenctionPoolClass;
    }

    public static ConcurrentHashMap<String, ClassItem> getMappingMap() {
        return mappingMap;
    }

    public static List<ClassItem> getMappingList() {
        List<ClassItem> classItemList = new ArrayList();
        Iterator iter = mappingMap.entrySet().iterator();

        while(iter.hasNext()) {
            Entry<String, ClassItem> entry = (Entry)iter.next();
            ClassItem classItem = (ClassItem)entry.getValue();
            classItemList.add(classItem);
        }

        return classItemList;
    }

    public static String getReLoadConfigXmlTime() {
        String reLoadConfigXmlTime = (String)constantsMap.get("reLoadConfigXmlTime");
        if (reLoadConfigXmlTime == null || "".equals(reLoadConfigXmlTime.trim())) {
            reLoadConfigXmlTime = "1000";
        }

        return reLoadConfigXmlTime;
    }

    public static void setReLoadConfigXmlTime(String reLoadConfigXmlTime) {
        constantsMap.put("reLoadConfigXmlTime", reLoadConfigXmlTime);
    }

    public static void setLogTime(String logTime) {
        constantsMap.put("logTime", logTime);
    }

    public static String getLogTime() {
        String logTime = (String)constantsMap.get("logTime");
        if (logTime == null || "".equals(logTime.trim())) {
            logTime = "1000";
        }

        return logTime;
    }

    public static void setLogBatchSize(String logBatchSize) {
        constantsMap.put("logBatchSize", logBatchSize);
    }

    public static long getLogBatchSize() {
        String logBatchSize = (String)constantsMap.get("logBatchSize");
        return logBatchSize != null && !"".equals(logBatchSize.trim()) ? Long.parseLong(logBatchSize) : 100L;
    }

    public static boolean isLogMode() {
        String logMode = (String)constantsMap.get("logMode");
        if (logMode == null || "".equals(logMode.trim())) {
            logMode = "true";
        }

        return "true".equals(logMode);
    }

    public static boolean isShowSql() {
        String showSql = (String)constantsMap.get("showSql");
        if (showSql == null || "".equals(showSql.trim())) {
            showSql = "false";
        }

        return "true".equals(showSql);
    }

    public static String getLogClass() {
        String logClass = (String)constantsMap.get("logClass");
        if (logClass == null || "".equals(logClass.trim())) {
            logClass = "com.vere.orm.log.BaseLog";
        }

        return logClass;
    }

    public static String getLogDirectory() {
        String logDirectory = (String)constantsMap.get("logDirectory");
        if (logDirectory == null || "".equals(logDirectory.trim())) {
            logDirectory = classAbsolutePath;
        }

        return logDirectory;
    }

    public static String getLogDatePattern() {
        String logDatePattern = (String)constantsMap.get("logDatePattern");
        if (logDatePattern == null || "".equals(logDatePattern.trim())) {
            logDatePattern = "yyyy-MM-dd hh:mm:ss";
        }

        return logDatePattern;
    }
}
